
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: module apiclient.mimeparse</title>
</head><body bgcolor="#f0f0f8">

<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong><a href="apiclient.html"><font color="#ffffff">apiclient</font></a>.mimeparse</strong></big></big> (version 0.1.3)</font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/usr/local/google/home/jcgregorio/projects/apiclient-release/apiclient/mimeparse.py">/usr/local/google/home/jcgregorio/projects/apiclient-release/apiclient/mimeparse.py</a></font></td></tr></table>
    <p><tt>MIME-Type&nbsp;Parser<br>
&nbsp;<br>
This&nbsp;module&nbsp;provides&nbsp;basic&nbsp;functions&nbsp;for&nbsp;handling&nbsp;mime-types.&nbsp;It&nbsp;can&nbsp;handle<br>
matching&nbsp;mime-types&nbsp;against&nbsp;a&nbsp;list&nbsp;of&nbsp;media-ranges.&nbsp;See&nbsp;section&nbsp;14.1&nbsp;of&nbsp;the<br>
HTTP&nbsp;specification&nbsp;[<a href="http://www.rfc-editor.org/rfc/rfc2616.txt">RFC&nbsp;2616</a>]&nbsp;for&nbsp;a&nbsp;complete&nbsp;explanation.<br>
&nbsp;<br>
&nbsp;&nbsp;&nbsp;<a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1">http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1</a><br>
&nbsp;<br>
Contents:<br>
&nbsp;-&nbsp;<a href="#-parse_mime_type">parse_mime_type</a>():&nbsp;&nbsp;&nbsp;Parses&nbsp;a&nbsp;mime-type&nbsp;into&nbsp;its&nbsp;component&nbsp;parts.<br>
&nbsp;-&nbsp;<a href="#-parse_media_range">parse_media_range</a>():&nbsp;Media-ranges&nbsp;are&nbsp;mime-types&nbsp;with&nbsp;wild-cards&nbsp;and&nbsp;a&nbsp;'q'<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quality&nbsp;parameter.<br>
&nbsp;-&nbsp;<a href="#-quality">quality</a>():&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Determines&nbsp;the&nbsp;quality&nbsp;('q')&nbsp;of&nbsp;a&nbsp;mime-type&nbsp;when<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compared&nbsp;against&nbsp;a&nbsp;list&nbsp;of&nbsp;media-ranges.<br>
&nbsp;-&nbsp;<a href="#-quality_parsed">quality_parsed</a>():&nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;like&nbsp;<a href="#-quality">quality</a>()&nbsp;except&nbsp;the&nbsp;second&nbsp;parameter&nbsp;must&nbsp;be<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre-parsed.<br>
&nbsp;-&nbsp;<a href="#-best_match">best_match</a>():&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Choose&nbsp;the&nbsp;mime-type&nbsp;with&nbsp;the&nbsp;highest&nbsp;quality&nbsp;('q')<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from&nbsp;a&nbsp;list&nbsp;of&nbsp;candidates.</tt></p>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#eeaa77">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
    
<tr><td bgcolor="#eeaa77"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><dl><dt><a name="-best_match"><strong>best_match</strong></a>(supported, header)</dt><dd><tt>Return&nbsp;mime-type&nbsp;with&nbsp;the&nbsp;highest&nbsp;quality&nbsp;('q')&nbsp;from&nbsp;list&nbsp;of&nbsp;candidates.<br>
&nbsp;<br>
Takes&nbsp;a&nbsp;list&nbsp;of&nbsp;supported&nbsp;mime-types&nbsp;and&nbsp;finds&nbsp;the&nbsp;best&nbsp;match&nbsp;for&nbsp;all&nbsp;the<br>
media-ranges&nbsp;listed&nbsp;in&nbsp;header.&nbsp;The&nbsp;value&nbsp;of&nbsp;header&nbsp;must&nbsp;be&nbsp;a&nbsp;string&nbsp;that<br>
conforms&nbsp;to&nbsp;the&nbsp;format&nbsp;of&nbsp;the&nbsp;HTTP&nbsp;Accept:&nbsp;header.&nbsp;The&nbsp;value&nbsp;of&nbsp;'supported'<br>
is&nbsp;a&nbsp;list&nbsp;of&nbsp;mime-types.&nbsp;The&nbsp;list&nbsp;of&nbsp;supported&nbsp;mime-types&nbsp;should&nbsp;be&nbsp;sorted<br>
in&nbsp;order&nbsp;of&nbsp;increasing&nbsp;desirability,&nbsp;in&nbsp;case&nbsp;of&nbsp;a&nbsp;situation&nbsp;where&nbsp;there&nbsp;is<br>
a&nbsp;tie.<br>
&nbsp;<br>
&gt;&gt;&gt;&nbsp;<a href="#-best_match">best_match</a>(['application/xbel+xml',&nbsp;'text/xml'],<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'text/*;q=0.5,*/*;&nbsp;q=0.1')<br>
'text/xml'</tt></dd></dl>
 <dl><dt><a name="-fitness_and_quality_parsed"><strong>fitness_and_quality_parsed</strong></a>(mime_type, parsed_ranges)</dt><dd><tt>Find&nbsp;the&nbsp;best&nbsp;match&nbsp;for&nbsp;a&nbsp;mime-type&nbsp;amongst&nbsp;parsed&nbsp;media-ranges.<br>
&nbsp;<br>
Find&nbsp;the&nbsp;best&nbsp;match&nbsp;for&nbsp;a&nbsp;given&nbsp;mime-type&nbsp;against&nbsp;a&nbsp;list&nbsp;of&nbsp;media_ranges<br>
that&nbsp;have&nbsp;already&nbsp;been&nbsp;parsed&nbsp;by&nbsp;<a href="#-parse_media_range">parse_media_range</a>().&nbsp;Returns&nbsp;a&nbsp;tuple&nbsp;of<br>
the&nbsp;fitness&nbsp;value&nbsp;and&nbsp;the&nbsp;value&nbsp;of&nbsp;the&nbsp;'q'&nbsp;quality&nbsp;parameter&nbsp;of&nbsp;the&nbsp;best<br>
match,&nbsp;or&nbsp;(-1,&nbsp;0)&nbsp;if&nbsp;no&nbsp;match&nbsp;was&nbsp;found.&nbsp;Just&nbsp;as&nbsp;for&nbsp;<a href="#-quality_parsed">quality_parsed</a>(),<br>
'parsed_ranges'&nbsp;must&nbsp;be&nbsp;a&nbsp;list&nbsp;of&nbsp;parsed&nbsp;media&nbsp;ranges.</tt></dd></dl>
 <dl><dt><a name="-parse_media_range"><strong>parse_media_range</strong></a>(range)</dt><dd><tt>Parse&nbsp;a&nbsp;media-range&nbsp;into&nbsp;its&nbsp;component&nbsp;parts.<br>
&nbsp;<br>
Carves&nbsp;up&nbsp;a&nbsp;media&nbsp;range&nbsp;and&nbsp;returns&nbsp;a&nbsp;tuple&nbsp;of&nbsp;the&nbsp;(type,&nbsp;subtype,<br>
params)&nbsp;where&nbsp;'params'&nbsp;is&nbsp;a&nbsp;dictionary&nbsp;of&nbsp;all&nbsp;the&nbsp;parameters&nbsp;for&nbsp;the&nbsp;media<br>
range.&nbsp;&nbsp;For&nbsp;example,&nbsp;the&nbsp;media&nbsp;range&nbsp;'application/*;q=0.5'&nbsp;would&nbsp;get&nbsp;parsed<br>
into:<br>
&nbsp;<br>
&nbsp;&nbsp;&nbsp;('application',&nbsp;'*',&nbsp;{'q',&nbsp;'0.5'})<br>
&nbsp;<br>
In&nbsp;addition&nbsp;this&nbsp;function&nbsp;also&nbsp;guarantees&nbsp;that&nbsp;there&nbsp;is&nbsp;a&nbsp;value&nbsp;for&nbsp;'q'<br>
in&nbsp;the&nbsp;params&nbsp;dictionary,&nbsp;filling&nbsp;it&nbsp;in&nbsp;with&nbsp;a&nbsp;proper&nbsp;default&nbsp;if<br>
necessary.</tt></dd></dl>
 <dl><dt><a name="-parse_mime_type"><strong>parse_mime_type</strong></a>(mime_type)</dt><dd><tt>Parses&nbsp;a&nbsp;mime-type&nbsp;into&nbsp;its&nbsp;component&nbsp;parts.<br>
&nbsp;<br>
Carves&nbsp;up&nbsp;a&nbsp;mime-type&nbsp;and&nbsp;returns&nbsp;a&nbsp;tuple&nbsp;of&nbsp;the&nbsp;(type,&nbsp;subtype,&nbsp;params)<br>
where&nbsp;'params'&nbsp;is&nbsp;a&nbsp;dictionary&nbsp;of&nbsp;all&nbsp;the&nbsp;parameters&nbsp;for&nbsp;the&nbsp;media&nbsp;range.<br>
For&nbsp;example,&nbsp;the&nbsp;media&nbsp;range&nbsp;'application/xhtml;q=0.5'&nbsp;would&nbsp;get&nbsp;parsed<br>
into:<br>
&nbsp;<br>
&nbsp;&nbsp;&nbsp;('application',&nbsp;'xhtml',&nbsp;{'q',&nbsp;'0.5'})</tt></dd></dl>
 <dl><dt><a name="-quality"><strong>quality</strong></a>(mime_type, ranges)</dt><dd><tt>Return&nbsp;the&nbsp;quality&nbsp;('q')&nbsp;of&nbsp;a&nbsp;mime-type&nbsp;against&nbsp;a&nbsp;list&nbsp;of&nbsp;media-ranges.<br>
&nbsp;<br>
Returns&nbsp;the&nbsp;quality&nbsp;'q'&nbsp;of&nbsp;a&nbsp;mime-type&nbsp;when&nbsp;compared&nbsp;against&nbsp;the<br>
media-ranges&nbsp;in&nbsp;ranges.&nbsp;For&nbsp;example:<br>
&nbsp;<br>
&gt;&gt;&gt;&nbsp;<a href="#-quality">quality</a>('text/html','text/*;q=0.3,&nbsp;text/html;q=0.7,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text/html;level=1,&nbsp;text/html;level=2;q=0.4,&nbsp;*/*;q=0.5')<br>
0.7</tt></dd></dl>
 <dl><dt><a name="-quality_parsed"><strong>quality_parsed</strong></a>(mime_type, parsed_ranges)</dt><dd><tt>Find&nbsp;the&nbsp;best&nbsp;match&nbsp;for&nbsp;a&nbsp;mime-type&nbsp;amongst&nbsp;parsed&nbsp;media-ranges.<br>
&nbsp;<br>
Find&nbsp;the&nbsp;best&nbsp;match&nbsp;for&nbsp;a&nbsp;given&nbsp;mime-type&nbsp;against&nbsp;a&nbsp;list&nbsp;of&nbsp;media_ranges<br>
that&nbsp;have&nbsp;already&nbsp;been&nbsp;parsed&nbsp;by&nbsp;<a href="#-parse_media_range">parse_media_range</a>().&nbsp;Returns&nbsp;the&nbsp;'q'<br>
quality&nbsp;parameter&nbsp;of&nbsp;the&nbsp;best&nbsp;match,&nbsp;0&nbsp;if&nbsp;no&nbsp;match&nbsp;was&nbsp;found.&nbsp;This&nbsp;function<br>
bahaves&nbsp;the&nbsp;same&nbsp;as&nbsp;<a href="#-quality">quality</a>()&nbsp;except&nbsp;that&nbsp;'parsed_ranges'&nbsp;must&nbsp;be&nbsp;a&nbsp;list&nbsp;of<br>
parsed&nbsp;media&nbsp;ranges.</tt></dd></dl>
</td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#55aa55">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
    
<tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><strong>__author__</strong> = 'Joe Gregorio'<br>
<strong>__credits__</strong> = ''<br>
<strong>__email__</strong> = 'joe@bitworking.org'<br>
<strong>__license__</strong> = 'MIT License'<br>
<strong>__version__</strong> = '0.1.3'</td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#7799ee">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr>
    
<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%">Joe&nbsp;Gregorio</td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#7799ee">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr>
    
<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"></td></tr></table>
</body></html>